package com.zhouyu;

/**
 * https://www.yuque.com/books/share/5f19528d-d89b-4b69-a7bd-9f436a2dd734/hczbgw
 */

import com.zhouyu.service.*;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.function.Supplier;

public class Test04 {
	public static void main(String[] args) {

		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		/**
		 * Conditional 测试
		 */
		/*System.out.println(context.getBean("conditionalBean"));*/

		/**
		 * LookUp 会生成一个动态代理类
		 */
		/*System.out.println(context.getBean("lookUpBean"));*/

		/**
		 * singleton prototype
		 */
		/*System.out.println(context.getBean("singletonOrPrototypeBean"));
		System.out.println(context.getBean("singletonOrPrototypeBean"));*/

		/**
		 * Lazy
		 */
		/*System.out.println(context.getBean("lazyBean"));*/

		/**
		 * Primary
		 */
		/*PrimaryOuterBean primaryOuterBean = (PrimaryOuterBean) context.getBean("primaryOuterBean");
		primaryOuterBean.test();*/

		/**
		 * DependsOn
		 */
		/*System.out.println(context.getBean("dependsOnBean"));*/

		/**
		 * 生成bean的时候判断 abstract
		 */
		/*ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		System.out.println(context.getBean("child"));
		System.out.println(context.getBean("child"));*/

		/**
		 * 实例化前
		 */
		/*System.out.println(context.getBean("userService"));*/

		/**
		 * 使用supplier创建对象
		 */
		/*AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
		beanDefinition.setInstanceSupplier(new Supplier<Object>() {
			@Override
			public Object get() {
				UserService userService = new UserService();
				System.out.println(userService);
				return userService;
			}
		});
		context.registerBeanDefinition("userService", beanDefinition);
		System.out.println(context.getBean("userService"));*/

		/**
		 * factory method creat Bean
		 */
		/*ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring.xml");
		System.out.println(classPathXmlApplicationContext.getBean("factoryMethodBean"));
		System.out.println(classPathXmlApplicationContext.getBean("userService1"));*/


		/**
		 * Lookup 注解使用
		 */
		/*LookUpBean lookUpBean = (LookUpBean) context.getBean("lookUpBean");
		System.out.println(lookUpBean);
		lookUpBean.test();*/

		/**
		 * BeanDefinition的后置处理 还有 实例化后的处理 InstantiationAwareBeanPostProcessor
		 */
		/*UserService userService = (UserService) context.getBean("userService");
		userService.printOrderService();*/

		/**
		 * 自动注入
		 */
		/*AutoInjectBean autoInjectBean = (AutoInjectBean) context.getBean("autoInjectBean");
		autoInjectBean.test();*/

		/**
		 * 实例化后的 设置属性 postProcessProperties
		 */
		/*UserService userService = (UserService) context.getBean("userService");
		userService.printxxx();*/

		/**
		 * 执行 BeanNameAware
		 */
		/*AwareBean awareBean = (AwareBean) context.getBean("awareBean");
		System.out.println(awareBean);*/


		/**
		 * PostConstruct 通过初始化前进行初始化一些代码
		 */
		UserService userService = (UserService) context.getBean("userService");
		System.out.println(userService);

		/**
		 * 初始化前 和 初始化后
		 */
		/*UserService userService = (UserService) context.getBean("userService");
		System.out.println(userService);*/




	}
}
